package cn.lingyangwl.agile.infra.file.modle.bo;

import cn.hutool.core.date.DateTime;
import cn.lingyangwl.agile.infra.module.file.model.entity.FileConfig;
import cn.lingyangwl.framework.tool.core.UrlUtils;
import lombok.Getter;
import lombok.Setter;

/**
 * 文件路径业务对象
 * @author shenguangyang
 */
public class FilePathBO {
    /**
     * 文件扩展, eg: jpg, png
     */
    @Getter
    private final String fileExtension;

    /**
     * 文件上传记录id
     */
    @Getter
    private final String fileRecordId;

    /**
     * 用户id
     */
    @Getter
    private final Long userId;

    @Setter
    private FileConfig fileConfig;

    /**
     * 新文件名
     */
    @Getter
    @Setter
    private String newName;

    public FilePathBO(FileConfig fileConfig, String fileExtension, String fileRecordId, Long userId) {
        this.fileConfig = fileConfig;
        this.fileExtension = fileExtension;
        this.fileRecordId = fileRecordId;
        this.userId = userId;
    }

    public String getPath() {
        String dataPath = new DateTime().toString("yyyy/MM/dd/");
        this.newName = fileRecordId + "." + fileExtension;
        return UrlUtils.removeRepeatSlashOfUrl(UrlUtils.removeStartsSlash(fileConfig.getPath() + "/" + dataPath + userId + "/" + newName));
    }

    public static FilePathBO parsePath(String path) {
        String[] split = path.split("/");
        String fileName = split[split.length - 1];
        String userId = split[split.length - 2];

        String[] fileNameSplit = fileName.split("\\.");
        String fileRecordId = fileNameSplit[0];
        String fileExtension = fileNameSplit[1];
        FilePathBO filePathBO = new FilePathBO(null, fileExtension, fileRecordId, Long.parseLong(userId));
        filePathBO.setNewName(fileName);
        return filePathBO;
    }


}
